from bs4 import BeautifulSoup
from wy.wy import items


# 输入文本内容 返回提取出的结果res_list 和标签集合 label_set
def resolve_file_content2019(cont, date_s):
    label_set = set()
    # cont = ''
    col_loc_dict = {2: '股票代码', 3: '名称'}
    # , 7: '涨停概念'
    res_list = []

    # with open('舞阳复盘 2019_9_25_15_7_15.html', 'r', encoding='utf-8') as f:
    #     while True:
    #         line = f.readline()  # 逐行读取
    #         if not line:
    #             break
    #         cont = ''.join([cont, line])
    # root = Selector(text=cont)

    soup = BeautifulSoup(cont)
    # print(soup.select("table[class='table table-hover .table-sm']"))

    for table in soup.select("table[class='table table-hover .table-sm']"):
        trs = table.tbody.findChildren()
        for tr in trs:
            tds = tr.findChildren();
            if (len(tds) >= 8):
                oneTP = items.TP_item()
                oneTP['code'] = tds[1].text;
                oneTP['name'] = tds[2].text;
                oneTP['full_time'] = date_s;
                oneTP['full_res'] = tds[7].text.replace(";", ",");
                res_list.append(oneTP)
                label_set.update(tds[7].text.split(";"))
        # tds = tables.tbody.tr.findChildren();
        # print(tds[1])
    # print('len:%s   list:%s' %(len(res_list),res_list))

    return label_set, res_list

    # base_path = '/html/body/div[2]/font/div[1]';
    # cell_path = base_path + '/table/tbody/tr[1]/td[1]/text()';

    # while not len(root.xpath(cell_path))==0:
    # cell = root.xpath(cell_path).extract()[0];

    # cell_path = base_path + '/table/tbody/tr[1]/td[1]/span/text()';
    # for key in col_loc_dict:
    #     f=cell_path.find('td[')
    #     cell_path = cell_path[:f+len("td['")-1]+str(key)+cell_path[f+len("td['"):]
    #     print('path:'+cell_path)
    #     cell_val_not = root.xpath(cell_path);
    #
    #     if len(cell_val_not) == 0:
    #         cell_path = base_path + '/table/tbody/tr[1]/td[1]/text()';
    #         f = cell_path.find('td[')
    #         cell_path = cell_path[:f + len("td['") - 1] + str(key) + cell_path[f + len("td['"):]
    #         print('path:' + cell_path)
    #
    #         cell_val_not = root.xpath(cell_path);
    #     if not len(cell_val_not)==0:
    #         cell_val = cell_val_not.extract()[0];
    #         print(cell_val)
    #         res_list.append({col_loc_dict[key]:cell_val})
    #

    # base_path = compact(base_path,'font','font/')
    # f = base_path.find("font");
    # base_path = base_path[:f]+'font/'+base_path[f:]
    # print('bp:'+base_path)
    #
    # print(res_list)
    # cell = root.xpath(cell_path).extract()[0];
    # print(cell)

    # ztyy = "军工;股权转让;实控人变更;"
    # ztyyr = ztyy.split(";")[:-1]
    # print(ztyyr)

    # 更新label 集合
    # label_set.update(ztyyr)
    # print(label_dict)

    # print(cell)


def resolve_file_content2018_11_27p(cont, date_s):
    return resolve_file_content2018_10_22p(cont, date_s)


def resolve_file_content2018_10_22p(cont, date_s):
    date_s = "0"
    label_set = set()
    cont = ''
    res_list = []

    with open('舞阳复盘 2019_1_9_14_57_14.html', 'r', encoding='utf-8') as f:
        while True:
            line = f.readline()  # 逐行读取
            if not line:
                break
            cont = ''.join([cont, line])
    # root = Selector(text=cont)

    soup = BeautifulSoup(cont)
    print(soup.select("table[class='m-table']"))

    for table in soup.select("table[class='m-table']"):
        trs = table.tbody.findChildren()
        for tr in trs:
            tds = tr.findChildren();
            if (len(tds) > 15):
                oneTP = items.TP_item()
                oneTP['code'] = tds[2].text;
                oneTP['name'] = tds[3].text;
                oneTP['full_time'] = date_s;
                oneTP['full_res'] = tds[15].text.replace(";", ",");
                res_list.append(oneTP)
                label_set.update(tds[15].text.split(";"))
        # tds = tables.tbody.tr.findChildren();
        # print(tds[1])
    # print('len:%s   list:%s' %(len(res_list),res_list))

    return label_set, res_list

if __name__ == "__main__":
    ls, rl = resolve_file_content2018_11_27p("", " ")
    print("标签集合:", ls)
    print("结果:", rl)
